#!/usr/bin/env python3
"""
Shopify API 模拟模块
提供模拟的Shopify API数据，用于开发和测试环境
"""

from datetime import datetime, timedelta
import random


def get_mock_products():
    """获取模拟的Shopify商品数据"""
    return [
        {
            'id': 123456789,
            'title': '定制3D打印模型 - 星空系列',
            'body_html': '<p>高质量3D打印模型，星空主题设计</p>',
            'status': 'active',
            'created_at': (datetime.now() - timedelta(days=10)).isoformat(),
            'updated_at': datetime.now().isoformat(),
            'price': '99.99',
            'variants': [
                {
                    'id': 987654321,
                    'price': '99.99',
                    'inventory_quantity': 50
                }
            ],
            'images': [
                {
                    'src': 'https://example.com/images/model1.jpg',
                    'position': 1
                },
                {
                    'src': 'https://example.com/images/model2.jpg',
                    'position': 2
                }
            ]
        },
        {
            'id': 987654321,
            'title': '3D定制钥匙扣 - 个性化礼品',
            'body_html': '<p>可定制的3D打印钥匙扣，完美的个性化礼物</p>',
            'status': 'active',
            'created_at': (datetime.now() - timedelta(days=5)).isoformat(),
            'updated_at': (datetime.now() - timedelta(days=2)).isoformat(),
            'price': '29.99',
            'variants': [
                {
                    'id': 123456789,
                    'price': '29.99',
                    'inventory_quantity': 100
                }
            ],
            'images': [
                {
                    'src': 'https://example.com/images/keychain1.jpg',
                    'position': 1
                }
            ]
        }
    ]


def get_mock_orders():
    """获取模拟的Shopify订单数据"""
    statuses = ['fulfilled', 'unfulfilled', 'partial', 'cancelled']
    payment_statuses = ['paid', 'pending', 'unpaid', 'partially_refunded']
    
    orders = []
    for i in range(1, 6):
        status = random.choice(statuses)
        payment_status = random.choice(payment_statuses)
        
        order = {
            'id': 100000000 + i,
            'name': f'#ABC{i:04d}',
            'created_at': (datetime.now() - timedelta(days=random.randint(1, 30))).isoformat(),
            'updated_at': datetime.now().isoformat(),
            'total_price': f'{random.uniform(50, 500):.2f}',
            'financial_status': payment_status,
            'fulfillment_status': status,
            'customer': {
                'first_name': f'Customer{i}',
                'last_name': f'User{i}',
                'email': f'customer{i}@example.com'
            },
            'shipping_address': {
                'address1': f'{random.randint(100, 999)} Main St',
                'city': 'New York',
                'province': 'NY',
                'zip': '10001',
                'country': 'United States'
            },
            'line_items': [
                {
                    'title': f'定制3D产品 #{i}',
                    'quantity': random.randint(1, 3),
                    'price': f'{random.uniform(29.99, 199.99):.2f}'
                }
            ]
        }
        
        # 添加物流信息（如果订单已发货）
        if status in ['fulfilled', 'partial']:
            order['fulfillments'] = [
                {
                    'tracking_number': f'TRK{i:08d}',
                    'tracking_company': 'FedEx'
                }
            ]
        
        orders.append(order)
    
    return orders


def get_mock_order(order_id):
    """获取单个模拟订单"""
    return {
        'id': int(order_id),
        'name': f'#ABC{int(order_id) % 1000:04d}',
        'created_at': (datetime.now() - timedelta(days=5)).isoformat(),
        'updated_at': datetime.now().isoformat(),
        'total_price': '149.99',
        'financial_status': 'paid',
        'fulfillment_status': 'fulfilled',
        'customer': {
            'first_name': 'Test',
            'last_name': 'Customer',
            'email': 'test@example.com'
        },
        'shipping_address': {
            'address1': '123 Test St',
            'city': 'Test City',
            'province': 'TC',
            'zip': '12345',
            'country': 'United States'
        },
        'line_items': [
            {
                'title': 'Premium 3D Model',
                'quantity': 1,
                'price': '149.99'
            }
        ],
        'fulfillments': [
            {
                'tracking_number': f'TRK{order_id}',
                'tracking_company': 'Test Express'
            }
        ]
    }


def mock_shopify_error(message="模拟的Shopify API错误"):
    """生成模拟的Shopify错误"""
    return {
        'errors': message,
        'code': 'API_ERROR',
        'status': 400
    }